<template>
	<view>
		<view class="hedtop" @click="openMap">
			<view class="cs">
				{{city}}
				<image src="../static/up.png" style="width:20rpx;height: 17rpx;margin-left: 5rpx;"></image>
			</view>
			<view class="seach">
				<u-search v-model="keyword" placeholder="搜索地址" shape="square" :clearabled="false" :animation="true"
					bg-color="#F5F5F5" height="75"></u-search>
			</view>
		</view>


		<view class="addlist">
			<view class="box" v-for="(item,index) in list" :key="index" :class="listIndex==index?'active':''"
				@click="changetan(index,item)">
				<view>
					<view>{{item.address}}</view>
					<view class="add">{{item.addressDetail}}</view>
				</view>
				<view v-if="listIndex==index">
					<image src="../static/duigou.png" style="width: 40rpx;height: 34rpx;"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				city: '',
				keyword: '',
				list: [],
				listIndex: '-1',
				index:0
			}
		},
		onLoad(option) {
			if(option.index){
				this.index = option.index
			}
			this.getAddlist()
		},
		onShow() {
			this.city = uni.getStorageSync('city')
		},
		methods: {
			openMap() { //打开地图选择位置
				let that = this
				uni.chooseLocation({
					success: function(res) {
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
						let data = {
							address: res.name, //地址
							addressDetail: res.address,
							addressLongitude: res.longitude, //地址经度
							addressLatitude: res.latitude, //地址维度
							addressId: '',
							addressDefault: '',
						}
						that.$Request.postJson('/app/indent/addUserAddress', data).then(res => {
							if (res.code === 0) {
								that.getAddlist()
							}
						});
					}
				});

			},

			getAddlist() {
				this.$Request.getT('/app/indent/findUserAddress').then(res => {
					if (res.code === 0) {
						this.list = res.data
					}
				});

			},
			changetan(index, item) {
				if(this.index==1){
					uni.setStorageSync('addressId', item.addressId)
					this.listIndex = index
					uni.navigateBack()
				}else if(this.index==2){
					uni.setStorageSync('addressIds', item.addressId)
					this.listIndex = index
					uni.navigateBack()
				}
				
			}
		}
	}
</script>

<style lang="less">
	page {
		background: #FFFFFF;
	}

	.hedtop {
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		position: fixed;
		/* #ifdef H5 */
		top: 85rpx;
		/* #endif */
		/* #ifndef H5 */
		top: 0rpx;
		/* #endif */
		left: 0;
		right: 0;
		z-index: 99;
		background: #FFFFFF;

		.cs {
			width: 20%;
		}

		.seach {
			width: 80%;
		}
	}

	.addlist {
		/* #ifdef H5 */
		margin-top: 120rpx;
		/* #endif */
		/* #ifndef H5 */
		margin-top: 120rpx;

		/* #endif */
		.box {
			// width: 100%;
			margin: 20rpx 30rpx;
			background: #F7F7F7;
			border-radius: 8rpx;
			padding: 30rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #333333;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.add {
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
				margin-top: 6rpx;
			}
		}

		.active {
			border: 2rpx solid #45AEFF;
			border-radius: 8rpx;
			background: #FFFFFF;
		}
	}
</style>